package com.byedu.utils;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TenpayUtil {

    private static Object Server;


    //商户相关资料
    public static String appid = "wx76923c18571682c8";//公众号appid
    public static String appsecret = "d0b870e4da8da9e356267a20f4b6e125";//公众号秘钥
    public static String partner = "1429454602";//商户号
    public static String partnerkey = "sjkiemgdsjbakxzeplsk147286521jss";//商户API秘钥


    public static String CREATEORDERURL = "https://api.mch.weixin.qq.com/pay/unifiedorder";


    public static String getUserInfoUrl(String access_token, String openid) {

        return "https://api.weixin.qq.com/sns/userinfo?access_token=" + access_token + "&openid=" + openid + "&lang=zh_CN";
    }


    public static String getRefreshUrl(String refresh_token) {
        return "https://api.weixin.qq.com/sns/oauth2/refresh_token?"
         + "appid=" + TenpayUtil.appid
         + "&grant_type=refresh_token&refresh_token="
         + refresh_token;
    }


//	public static String getRedirect_baseurl(String redirect_uri){
//		try {
//			redirect_uri = URLEncoder.encode(
//					redirect_uri, "UTF-8");
//		} catch (UnsupportedEncodingException e) {
//			e.printStackTrace();
//		}// 授权后重定向的回调链接地址，请使用urlencode对链接进行处理（文档要求）
//        // 按照文档要求拼接访问地址
//        return "https://open.weixin.qq.com/connect/oauth2/authorize?appid="
//                + TenpayUtil.appid
//                + "&redirect_uri="
//                + redirect_uri
//                + "&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect";
//
//	}

    public static String getRedirect_userinfourl(String redirect_uri) {
        try {
            redirect_uri = URLEncoder.encode(
             redirect_uri, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }// 授权后重定向的回调链接地址，请使用urlencode对链接进行处理（文档要求）
        // 按照文档要求拼接访问地址
        return "https://open.weixin.qq.com/connect/oauth2/authorize?appid="
         + TenpayUtil.appid
         + "&redirect_uri="
         + redirect_uri
         + "&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";

    }

    // access_token
    public static String getAccess_tokenUrl(String code) {
        return "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + TenpayUtil.appid + "&secret=" + TenpayUtil.appsecret + "&code=" + code + "&grant_type=authorization_code";
    }


    /**
     * 把对象转换成字符串
     *
     * @param obj
     * @return String 转换成字符串,若对象为null,则返回空字符串.
     */
    public static String toString(Object obj) {
        if (obj == null)
            return "";

        return obj.toString();
    }

    /**
     * 把对象转换为int数值.
     *
     * @param obj 包含数字的对象.
     * @return int 转换后的数值,对不能转换的对象返回0。
     */
    public static int toInt(Object obj) {
        int a = 0;
        try {
            if (obj != null)
                a = Integer.parseInt(obj.toString());
        } catch (Exception e) {

        }
        return a;
    }

    /**
     * 获取当前时间 yyyyMMddHHmmss
     *
     * @return String
     */
    public static String getCurrTime() {
        Date now = new Date();
        SimpleDateFormat outFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        String s = outFormat.format(now);
        return s;
    }

    /**
     * 获取当前日期 yyyyMMdd
     *
     * @param date
     * @return String
     */
    public static String formatDate(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
        String strDate = formatter.format(date);
        return strDate;
    }

    /**
     * 取出一个指定长度大小的随机正整数.
     *
     * @param length int 设定所取出随机数的长度。length小于11
     * @return int 返回生成的随机数。
     */
    public static int buildRandom(int length) {
        int num = 1;
        double random = Math.random();
        if (random < 0.1) {
            random = random + 0.1;
        }
        for (int i = 0; i < length; i++) {
            num = num * 10;
        }
        return (int) ((random * num));
    }

    /**
     * 获取编码字符集
     *
     * @param request
     * @param response
     * @return String
     */

    public static String getCharacterEncoding(HttpServletRequest request,
                                              HttpServletResponse response) {

        if (null == request || null == response) {
            return "utf-8";
        }

        String enc = request.getCharacterEncoding();
        if (null == enc || "".equals(enc)) {
            enc = response.getCharacterEncoding();
        }

        if (null == enc || "".equals(enc)) {
            enc = "utf-8";
        }

        return enc;
    }

    public static String URLencode(String content) {

        String URLencode;

        URLencode = replace(Server.equals(content), "+", "%20");

        return URLencode;
    }

    private static String replace(boolean equals, String string, String string2) {

        return null;
    }

    /**
     * 获取unix时间，从1970-01-01 00:00:00开始的秒数
     *
     * @param date
     * @return long
     */
    public static long getUnixTime(Date date) {
        if (null == date) {
            return 0;
        }

        return date.getTime() / 1000;
    }

    public static String QRfromGoogle(String chl) {
        int widhtHeight = 300;
        String EC_level = "L";
        int margin = 0;
        String QRfromGoogle;
        chl = URLencode(chl);

        QRfromGoogle = "http://chart.apis.google.com/chart?chs=" + widhtHeight + "x" + widhtHeight + "&cht=qr&chld=" + EC_level + "|" + margin + "&chl=" + chl;

        return QRfromGoogle;
    }

    /**
     * 时间转换成字符串
     *
     * @param date       时间
     * @param formatType 格式化类型
     * @return String
     */
    public static String date2String(Date date, String formatType) {
        SimpleDateFormat sdf = new SimpleDateFormat(formatType);
        return sdf.format(date);
    }

}


